#coding=utf-8
#!/usr/bin/python3
# @author:ZhaiGuiDe
# @Time:2021/6/15/ 15:24

import requests
import gevent

from constants import ADMIN_IP
from load_video_110_53.constants import ADMIN_PORT, COLLECT_VIDEO_URL
from load_video_110_53.db_connect import MysqlClient
from load_video_110_53.load_video_110_53_81 import ProcessVideo


class CllectVideo:
    """
    查询所有包含关键帧和识别结果的视频
    """
    def __init__(self):
        self.mycli = MysqlClient()
        self.prv = ProcessVideo()

    def get_ai_keyframe_events_id(self):
        # 查询到包含文本分析和关键帧数识别的视频de ID
        id_list = []
        # select_join_on_sql = """
        #     SELECT DISTINCT(vo.id) FROM `video_origin` AS vo, ai_keyframe AS ak, ai_events AS ae WHERE  vo.id = ae.video_id and vo.id=ak.video_id AND create_by = "admin2000"
        # """
        select_join_on_sql ="""
                        SELECT  ak.recpeople,ak.video_id FROM `video_origin` AS vo, ai_keyframe AS ak  where vo.id=ak.video_id AND create_by = "admin2000"
                    """
        result = self.mycli.dataSelect(select_join_on_sql)
        # print(result[0])
        rows = [
            {f'{row[1]}': f'{row[0]}'} for row in result
        ]
        import json
        print(json.dumps(rows[:3], ensure_ascii=False))

    def cllect_video(self,video_id):
        url = "http://"+ADMIN_IP+":"+ADMIN_PORT + COLLECT_VIDEO_URL
        headers = {
            "Accept":"application/json, text/plain, */*",
            "Accept-Encoding":"gzip, deflate",
            "Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8",
            "Cache-Control":"no-cache",
            "Connection":"keep-alive",
            "Content-Length":"13",
            "Content-Type":"application/x-www-form-urlencoded",
            "Host": ADMIN_IP + ":" + ADMIN_PORT,
            "Origin":"http://"+ADMIN_IP+":"+ADMIN_PORT,
            "Pragma":"no-cache",
            "Referer":"http://" + ADMIN_IP + ":" + ADMIN_PORT +"/",
            "token":self.prv.get_token(),
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4449.6 Safari/537.36",
        }
        data = {
            "videoId":video_id
        }
        # print("+"*10,url)
        print(headers)
        # print(data)
        res = requests.post(url=url,headers=headers,data=data)
        print(res.json())

if __name__ == '__main__':
    cv = CllectVideo()
    cv.get_ai_keyframe_events_id()
    # for video_id in cv.get_ai_keyframe_events_id():
    #     gevent.joinall([
    #         gevent.spawn(cv.cllect_video,video_id)
    #     ])
        # cv.cllect_video(video_id)









